import { CurrentUser } from '@/decorators';
import {
  MessageCreateDto,
  MessageFindDto,
  MessageListDto,
  MessageQueryDto,
  MessageRemoveDto,
  MessageUpdateDto,
} from '@/dtos';
import { MessageService } from '@/services/message.service';
import { ok } from '@/utils';
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
} from '@nestjs/common';
import { BaseController } from './base.controller';

@Controller('message')
export class MessageController extends BaseController {
  constructor(private readonly messageService: MessageService) {
    super();
  }

  @Post('list')
  async getList(
    @Body() dto: MessageListDto,
    @CurrentUser('id') userId: number,
  ) {
    return ok(await this.messageService.getList(userId, dto));
  }

  @Get('detail/:id')
  async getDetail(@Param('id') id: number) {
    return ok(
      await this.messageService.findOne(
        {
          id,
          isDeleted: false,
        },
        {
          id: true,
          title: true,
          type: true,
          content: true,
          createdAt: true,
        },
      ),
    );
  }

  @Post('admin/query')
  async query(@Body() dto: MessageQueryDto) {
    return ok(await this.messageService.query(dto));
  }

  @Post('admin/find')
  async find(@Body() dto: MessageFindDto) {
    return ok(await this.messageService.findOne(dto.where, dto.select));
  }

  @Post('admin/create')
  async create(@Body() dto: MessageCreateDto) {
    return ok(await this.messageService.create(dto.data));
  }

  @Patch('admin/update')
  async update(@Body() dto: MessageUpdateDto) {
    return ok(await this.messageService.update(dto));
  }

  @Delete('admin/remove')
  async remove(@Query() dto: MessageRemoveDto) {
    return ok(await this.messageService.remove(dto));
  }
}
